/*
 *   Copyright (C) <2009> 
 *
 *   <Sandeep K Sinha>
 *   <Rishi B Agrawal> <Vineet Agarwal> <Rohit Vashist> <Rohit K Sharma> <Sneha Hendre> 
 *   
 *   <sandeepksinha@gmail.com> <rishi.b.agrawal@gmail.com> <checkout.vineet@gmail.com> 
 *   <rohit.k.vashist@gmail.com> <imreckless@gmail.com> <sneha.hendre@gmail.com>
 *
 *   This program is free software: you can redistribute it and/or modify it 
 *   under the terms of the GNU General Public License as published by the 
 *   Free Software Foundation, either version 3 of the License, or (at your 
 *   option) any later version->
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE->  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

#ifndef _OHSM_H_
#define _OHSM_H_

/* ioctls */
#define IOC_OHSM_ENABLE 1 
#define OHSM_RELOCATION 2 
#define OHSM_SELECT_RELOCATION 3
#define DISABLE_OHSM 7 
#define OHSM_UPDATE_TARGET_INFO 21 
#define OHSM_LIB_DEV_MAPPER 22  
#define OHSM_GET_TIER_INFO 23
#define OHSM_GET_ALLOC_POL 24
#define OHSM_GET_RELOC_POL 25
#define OHSM_SET_TIER_INFO 26
#define OHSM_SET_ALLOC_POL 27
#define OHSM_SET_RELOC_POL 28
#define DISPLAY_SAM 31 
#define DISPLAY_INODES 32 
#define OHSM_STATUS 33 
#define DEMO_RELOC 34

#define NR_CRITERIAS 5 
#define OHSM_MOUNT_POINT_LENGTH 20 
#define MAX_TIER_ALLOWED 11
#define TRUE 1 
#define FALSE 0 
#define OHSM_MAX_TARGET_ALLOWED 100 
#define MAX_DEV_NAME_LEN 20 
#define MAX_EXTENTION_LENGTH 5 
#define SET 1 
#define UNSET 0 

/*Relocation criteria*/
#define FSIZE 	0x01 
#define FAA 	0x02 
#define FMA 	0x04 
#define FIOT 	0x08 
#define FAT 	0x10 

/*Conditons*/
#define LT 1 
#define GT 2 
#define EQ 3 
#define NEQ 4 
#define LE 5 
#define GE 6  

/*Error types*/
enum ohsm_error {

	OHSM_ERROR = -1,
	OHSM_SUCCESS,
	OHSM_ERR_ADMIN, 
	OHSM_ERR_ALLOC_PTR, 
	OHSM_ERR_ALLOC, 
	OHSM_ERR_RELOC_PTR,  
	OHSM_ERR_RELOC, 
	OHSM_ERR_RELOCATE, 
	OHSM_ERR_RULE_BITMAP, 
	OHSM_ERR_ALREADY_DISABLED, 
	OHSM_ERR_OHSM_INACTIVE, 
	OHSM_ERR_RELOC_ON, 
	OHSM_ERR_RELOC_INACTIVE, 
	OHSM_ERR_ALREADY_EN, 
	OHSM_ERR_ENOMEM, 
	OHSM_ERR_FS,
	OHSM_ERR_NULL_PTR,
	OHSM_ERR_INO_NOEXIST, 
	OHSM_ERR_MNTPNT, 
	OHSM_ERR_DISP_DEVICE,
	OHSM_ERR_DISP_ALLOC,
	OHSM_ERR_DISP_RELOC 
};


/*
 * Option flags
 */
 
#define OPT_FLAG_H	0x00000000	/* Enable OHSM 	*/
#define OPT_FLAG_E	0x00000001	/* Enable OHSM 	*/
#define OPT_FLAG_D	0x00000002	/* Disable OHSM */
#define OPT_FLAG_N	0x00000004	/* Get State 	*/
#define OPT_FLAG_V	0x00000008	/* Debug ioctl	*/	
#define OPT_FLAG_R	0x00000010	/* Relocate		*/
#define OPT_FLAG_I	0x00000020	/* Device info	*/
#define OPT_FLAG_A	0x00000040	/* Alloc policy	*/
#define OPT_FLAG_P	0x00000080	/* Reloc policy	*/
#define OPT_FLAG_S	0x00000100	/* Set value	*/	
#define OPT_FLAG_G	0x00000200	/* Get value	*/
#define OPT_FLAG_W	0x00000400	/* Device name	*/ 
#define OPT_FLAG_X	0x00000800	/* Tier file	*/
#define OPT_FLAG_Y	0x00001000	/* Alloc file	*/
#define OPT_FLAG_Z	0x00002000	/* Reloc file	*/
#define OPT_FLAG_M	0x00004000	/* Mountpoint	*/
#define OPT_FLAG_T	0x00008000	/* Sam table	*/
#define OPT_FLAG_F	0x00010000	/* Files		*/
#define OPT_FLAG_O	0x00020000  /* Options Rules  */

#define OHSM_HELP				OPT_FLAG_H
#define OHSM_ENABLE				(OPT_FLAG_E | OPT_FLAG_W | OPT_FLAG_X | OPT_FLAG_Y | OPT_FLAG_Z | OPT_FLAG_M)
#define OHSM_DISABLE			OPT_FLAG_D
#define OHSM_RELOCATE			OPT_FLAG_R
#define OHSM_SELECT_RELOCATE  	OPT_FLAG_R | OPT_FLAG_O
#define OHSM_STATE 				OPT_FLAG_N
#define GET_ALLOC_POLICY		OPT_FLAG_G | OPT_FLAG_A
#define GET_RELOC_POLICY		OPT_FLAG_G | OPT_FLAG_P
#define GET_TIER_INFO			OPT_FLAG_G | OPT_FLAG_I
#define SET_DEVICE_INFO			OPT_FLAG_S | OPT_FLAG_X | OPT_FLAG_W
#define SET_ALLOC_POLICY		OPT_FLAG_S | OPT_FLAG_Y
#define SET_RELOC_POLICY		OPT_FLAG_S | OPT_FLAG_Z
#define DEBUG_RELOCATE			OPT_FLAG_V | OPT_FLAG_R  
#define DEBUG_GET_SAM			OPT_FLAG_G | OPT_FLAG_T  
#define DEBUG_GET_FILES			OPT_FLAG_G | OPT_FLAG_F  

/*
 * Data structure used for allocation policy.
 */

struct ohsm_uid_tier {
	uid_t uid;					/* user id */
	unsigned char tier;				/* respective tier of the user */
};

struct ohsm_gid_tier {
	gid_t gid;		 			/* group id*/
	unsigned char tier;				/* respective tier of the group */
};

struct ohsm_type_tier {
	char type[MAX_EXTENTION_LENGTH];		/* file extention */
	unsigned  char tier;				/* respective tier for the file type */
};

struct ohsm_allocation_policy {
	struct ohsm_uid_tier uid_tier[100];			/* address of dyamically created ohsm_uid_tier */ 
	struct ohsm_gid_tier gid_tier[100];			/* address of dynamically created ohsm_gid_tier */
	struct ohsm_type_tier type_tier[100];		/* address of dynamically created ohsm_type_tier*/
	int uidx; // Remove th 
	int gidx;
	int tidx;
	int cnt_users;					/* count of users for which tier has been specified*/
	int cnt_groups;					/* count of groups for which tier has been specified*/
	int cnt_types;					/* count of file types for which tier has been specified*/
};

/*
 * Data structure used for relocation policy
 */

struct ohsm_relocation_policy {
	unsigned char reloc_criteria_bitmap;		/* bitmap for criterias specified in the policy*/	
	unsigned char src_tier;				/* the source tier for this policy */
	unsigned char dest_tier;			/* the destination tier for this table */
	unsigned char rel_ops[NR_CRITERIAS];		/* the relational operators for the criterias entered*/
	long long values[NR_CRITERIAS];			/* the values for the criterias of the policy*/
};

struct ohsm_reloc_policy {
	struct ohsm_relocation_policy policy[100];
	int cnt_policies;
	int pidx; 
};	

struct ohsm_reloc_policy *relocpol;


/*
 * Data structure to demo relocation, 
 * passed through ioctl 
 */
struct ohsm_debug_reloc {
	unsigned long inum;			/* Inode Number */ 
	unsigned int dest;			/*Destination */
};

struct ohsm_device_info {
	    unsigned int dev_num;                  /* device number*/
	    unsigned char tier;
	    unsigned long begin;                /* begining of the device*/
	    unsigned long len;              /* length of the device*/
	    unsigned int blkgrp_start;          /* start block group of the device*/
	    unsigned int blkgrp_end;            /* end block group of the device*/
		char dev_name[MAX_DEV_NAME_LEN];		/* device name*/
};

struct ohsm_tier_dev_info {
		unsigned int dinfo_count;
		unsigned int nr_tiers;
		unsigned int nr_devices;
		struct ohsm_device_info device[100];
};

struct ohsm_tier_dev_info *dev_info;

/*
 * Data structure used for sending the ioctl,
 * during ohsm enable
 */

struct ohsm_struct_for_ioctl {
	struct ohsm_tier_dev_info *ptr_dev_file;
	struct ohsm_reloc_policy *ptr_reloc_policy;	
	struct ohsm_allocation_policy *ptr_alloc_policy;
	char mountpoint[OHSM_MOUNT_POINT_LENGTH];
};
#endif
